package com.cn.wanxi.servlet.back.nav;

import com.cn.wanxi.model.NavModel;
import com.cn.wanxi.service.INavService;
import com.cn.wanxi.service.impl.NavServiceImpl;
import com.cn.wanxi.util.DataEncoding;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

@WebServlet("/back/nav/manage")
public class NavManageServlet extends HttpServlet {
    INavService navService = new NavServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        DataEncoding.setTextContentType(resp);
        DataEncoding.setCharacterEncoding(req,resp);
        String nid = req.getParameter("id");
        int id = 0;
        if (!"".equals(nid)) {
            id = Integer.parseInt(nid);
        }
        int result = navService.deleteNav(id);
        resp.getWriter().print(result);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        DataEncoding.setTextContentType(resp);
        DataEncoding.setCharacterEncoding(req,resp);
        NavModel navModel = new NavModel();
        String nid = req.getParameter("id");
        int id = 0;
        if (!"".equals(nid))
            id = Integer.parseInt(nid);
        navModel.setId(id);
        navModel.setTitle(req.getParameter("title"));
        navModel.setHref(req.getParameter("href"));
        navModel.setUpdate_time(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date().getTime()));
        int result = navService.editNav(navModel);
        resp.getWriter().print(result);
    }
}
